package com.membership.stumembership.contrller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.membership.stumembership.domain.Membership;
import com.membership.stumembership.domain.Student;
import com.membership.stumembership.service.MembershipService;
import com.membership.stumembership.service.StudentService;
import com.membership.stumembership.utils.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/membership")
@RequiredArgsConstructor
public class MembershipController {
    //@Autowired
//@Resource
    private final MembershipService membershipService;
    private final StudentService studentService;

    @RequestMapping("/getAll")
    public Object selectAll() {
        return membershipService.list((Wrapper<Membership>) null);
    }

    @RequestMapping("/selectMemByPage")
    public Object selectByMemPage(Membership mem, Page<Membership> page) {
        return membershipService.selectByMemPage(page, mem);
    }

    @RequestMapping("/getAllVue")
    public Object getAllVue(Page<Membership> page) {
        return new Result(0, "成功", membershipService.page(page), (int) membershipService.count());
    }

    @RequestMapping("/add")
    public Object add(@RequestBody Membership membership) {
        Membership mspe =
                membershipService.getOne(new QueryWrapper<Membership>().eq("mspe", membership.getMspe()));
        if (mspe != null) {
            return new Result(0, "添加失败，该系统已存在！");
        }
        return membershipService.save(membership);
    }
    @RequestMapping("/delete")
    public Object delete(@RequestBody Membership mem){
        List<Student> list = studentService.list(new QueryWrapper<Student>().eq("mid", mem.getMid()));
        if(!list.isEmpty()){
            return  new Result(0,"该学院下面有学生，不能删除！");
        }
        return membershipService.removeById(mem.getMid());
    }

    @RequestMapping("/update")
    public Object update(@RequestBody Membership mem){
        return membershipService.updateById(mem);
    }
}


